#include "SWKNXProtocol.h"


SWKNXProtocol::SWKNXProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::SWKNXProtol;
}

QList<Package> SWKNXProtocol::parseData(QByteArray data)
{
    quint8  checkSteps = CHECK_HEAD;          //当前解析状态
    quint16 lengthCount;                 //数据长度计算

    quint16 bodyLength;
    QByteArray dataArray;
    quint16 crc16=0;
    QList<Package> packageList;
 //   qDebug()<<"SWKNXProtocol cheaking now----------------------";
    for(int i=0; i<data.size(); i++)
    {
        quint8 byte = data.at(i);
        switch(checkSteps)
        {
            case CHECK_HEAD:   //检查数据头

                 if(byte == 0XEA)
                 {

                    lengthCount = 0;
                    bodyLength = 0;
                    dataArray.clear();
                    dataArray.append(0xEA);
                    checkSteps = CHECK_LENGTH;
                    qDebug()<<"0xea";
                 }
            break;

            case CHECK_LENGTH:
                 if(byte < 2)
                     checkSteps = CHECK_HEAD;
                 else
                 {
                 checkSteps = CHECK_FUNCTION;
                 dataArray.append(byte);
                 bodyLength = byte;
                 }
                 break;

            case CHECK_FUNCTION:

                 dataArray.append(byte);
                 checkSteps = CHECK_BODY;
                lengthCount=0;
                if(bodyLength == 2)
                    checkSteps = CHECK_CHECKCODE;
                break;
            case CHECK_BODY:    //接收数据
                     lengthCount++;
                 dataArray.append(byte);
                 if(lengthCount==bodyLength-2)

                    {
                     lengthCount = 0;
                     checkSteps = CHECK_CHECKCODE;
                    }

                 break;

            case CHECK_CHECKCODE:

                 dataArray.append(byte);

               //  if(lengthCount == 2)
                 {
                     lengthCount = 0;
                     quint8 sum = CheckCode::computeSum(dataArray,0,dataArray.size()-1);
                   //  printf("%d,%02X, %02X\r\n",dataArray.size(),CheckCode::computeSum(dataArray,0,dataArray.size()-1)+0xa5,byte);
                     if((quint8)(sum+0xa5) == byte)
                     {
                         Package package;
                         package.setHead(dataArray.mid(0,1));
                         package.setLength(dataArray.mid(1,1).toHex().toInt(0,16));
                         package.setFunction(dataArray.mid(2,1).toHex().toInt(0,16));

                         if(bodyLength > 2)
                         package.setBody(dataArray.mid(3,bodyLength-2));
                         package.setCheckCode(dataArray[dataArray.size()-1]);
                         packageList.append(package);
                      //   qDebug()<<"cmdbody="<<DataTypeConver::formatString(package.body());
                     }
                     checkSteps = CHECK_HEAD;
                 }
        }
    }

    return packageList;
}

QByteArray SWKNXProtocol::generateData(Package package)
{

    QByteArray head = QByteArray::fromHex(QString("EA").toLatin1());
    quint8 length  = package.body().length()+2;

    package.setHead(head);
    package.setLength(length);

    QByteArray data;
    data.append(package.head());

    data.append(package.length());
    data.append(package.function());
    data.append(package.body());

    quint8 sum = CheckCode::computeSum(data,0,data.size());
    data.append(sum+0xA5);

    return data;
}
